define(function(require,exports,module){
    'use strict';

    var React = require('react');
    var $ = require('jquery');
    var bootstrap = require('bootstrap');
    var language = require('datetimepickerLanguage');
    var datetimepicker = require('datetimepicker');
    var publicObj = require('lib/public');
    var token = publicObj.get_token();

    //调用UserAreaDiagram组件
    var UserAreaDiagram = require('components/diagram/AreaBasicDiagram');
	var ReactLoading = require('../../partial/ReactLoading');
	
    module.exports = React.createClass({
        loadData:function(start_date,end_date){
            if(start_date && end_date){

                this.setState({'start_date':start_date});
                this.setState({'end_date':end_date});
                this.setState({'data':{'ios':[],'android':[]}});

                var sql = "SELECT FROM_UNIXTIME(ctime, '%Y-%m-%d') as name,\
                            count(*) as value\
                            FROM qeeniao.user\
                            where ctime > unix_timestamp('"+start_date+" 00:00:00')\
                            and ctime <= unix_timestamp('"+end_date+" 00:00:00')\
                            group by name\
                            order by name ASC";

                $.ajax({
                    url:this.props.url,
                    data:{sql:sql,'access_token':token},
                    type:'post',
                    dataType:'json',
                    success:function(o){
                        var tmp = [];
                        tmp.push(o);
                        if(o){
                            if(this.isMounted()) {
                                this.setState({'data': tmp});
                            }
                        }

                    }.bind(this),
                    error:function(xhr,status,err){
                        console.error(sql,status,err.toString());
                    }.bind(this)
                });
            }
        },
        handleSubmit:function(e){
            var start_date = React.findDOMNode(this.refs.from_date).value.trim();
            var end_date = React.findDOMNode(this.refs.to_date).value.trim();
			this.setState({'start':true});
            this.loadData(start_date,end_date);
        },
        componentDidMount:function(){
            $('#day_register_from_date').datetimepicker({
                language:'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2
            }).on('changeDate',function(ev){
                $('#day_register_to_date').datetimepicker('setStartDate',ev.date);
            }).on('outOfRange',function(){

            });

            $('#day_register_to_date').datetimepicker({
                language:'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2
            }).on('changeDate',function(ev){
                $('#day_register_from_date').datetimepicker('setEndDate',ev.date);
            }).on('outOfRange',function(){

            });
        },
        getInitialState:function(){
            return {
                'data':[],
                'start_date':'',
                'end_date':'',
				'start':false
            };
        },
        render:function(){
            var diagram;
            if(this.state.data.length > 0){
                diagram = this.state.data.map(function(item){
                    return (
                        <UserAreaDiagram className="" id="MoreUserAreaDiagram" id_name="MoreUserAreaDiagram" y="注册量" title="用户注册量" data={item} />

                    );
                });
            }else{
				if(this.state.start){
					diagram = <ReactLoading type='spinning-bubbles' color='#e3e3e3'/>
				}
            }

            return (
                <div className="modal fade" id={this.props.id} tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div className="modal-dialog">
                        <div className="modal-content">
                            <div className="modal-header">
                                <button type="button" className="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 className="modal-title" id="myModalLabel">更多图表</h4>
                            </div>

                            <div className="modal-body">
                                <div className="container">
                                    <form className="form-inline" id="dateForm" onSubmit={this.handleSubmit}>
                                        <div className="row">
                                            <div className="form-group">
                                                <label htmlFor="from_date">选择日期：</label>
                                                <input onChange={this.handleStartDate} className="form-control" size="16" type="text" value={this.state.start_date} id="day_register_from_date" ref="from_date" readOnly="true" data-date="" data-date-format="yyyy-mm-dd" data-link-field="dtp_input2" data-link-format="yyy-mm-dd"/>
                                            </div>

                                            <div className="form-group">
                                                <label htmlFor="to_date"> - </label>
                                                <input onChange={this.handleEndDate} className="form-control" size="16" type="text" value={this.state.end_date} id="day_register_to_date" ref="to_date" readOnly="true" data-date="" data-date-format="yyyy-mm-dd" data-link-field="dtp_input2" data-link-format="yyy-mm-dd"/>
                                            </div>

                                            <button type="submit" style={{marginLeft:'10px'}} className="btn btn-default btn-primary">提交</button>
                                        </div>
                                    </form>
                                </div>
                            </div>

                            <div className="modal-body">
                                <div className="row">
                                    {diagram}
                                </div>
                            </div>

                            <div className="modal-footer">
                                <button type="button" className="btn btn-default" data-dismiss="modal">退出</button>
                            </div>
                        </div>
                    </div>
                </div>
            );
        }
    });

});